package MyGui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

import Element.*;
import DistSys.*;

/**
 * ButtonPanel is a panel containing all Buttons, for zooming and drawing
 */
public class ButtonPanel extends JPanel implements ActionListener{

    MainFrame mainFrame;  //MainApplet

    public JLabel  lblLoading;
    public JProgressBar progressBar;
    private JPanel  subPanelOne, subPanelTwo, subPanelThree,
                    subPanelFour, subPanelFive, subPanelSix;

    //for progress bar mode
    static final int OPEN_MODE=0, SAVE_MODE=1, CONN_MODE=2, COPY_MODE=3, ENGN_MODE=4;

    final int
        BTN_ARROW=0, BTN_ZOOM_WIN=1, BTN_ZOOM_ALL=2, BTN_ZOOM_PREV=3,
        BTN_SOURCE=4, BTN_BUS=5, BTN_LINE=6, BTN_TXFM=7,
        BTN_DG=8,  BTN_TXFM_3=9, BTN_CABLE=10,BTN_REG=11,
        BTN_CKT_BK=12, BTN_RECL=13, BTN_FUSE=14, BTN_SWITCH_3=15,
        BTN_BK=16,BTN_SECT=17,BTN_SWITCH=18,BTN_SWITCH_GND=19,
        BTN_LOAD=20, BTN_SERIES_CAP=21,BTN_CAP=22,BTN_CT=23,
        BTN_MOTOR=24, BTN_REACTOR=25,BTN_ARRESTER=26,BTN_PT=27,
        BTN_GROUP=28,BTN_LOCK=29;
    final int  NUM_OF_BTNS = 30;
    boolean isLocked=false;

    JToggleButton[] btnArray= new JToggleButton[NUM_OF_BTNS];

    int     idOfDrawingBtn;

    public ButtonPanel() {
        this.setLayout(new FlowLayout(FlowLayout.LEFT));

        Insets zeroInsets = new Insets(1,1,1,1);
        KeyStroke postTip = KeyStroke.getKeyStroke(KeyEvent.VK_F1,
            Event.CTRL_MASK);
        for(int i=0;i<NUM_OF_BTNS;i++){
            btnArray[i] = new JToggleButton(GuiConst.icons[i]);
            btnArray[i].setToolTipText(GuiConst.iconTips[i]);
            // Remove shortcut again which was added in setToolTipText
            // since we need it otherwise
            btnArray[i].getInputMap(JComponent.WHEN_FOCUSED).remove(postTip);
            btnArray[i].setMargin(zeroInsets);
        }

        subPanelOne= new JPanel( new GridLayout(2,2));
        for(int i=0;i<4;i++) subPanelOne.add(btnArray[i]);

        subPanelTwo=new JPanel(new GridLayout(2,4));
        for(int i=4;i<12;i++) subPanelTwo.add(btnArray[i]);

        subPanelThree=new JPanel(new GridLayout(2,4));
        for(int i=12;i<20;i++) subPanelThree.add(btnArray[i]);

        subPanelFour=new JPanel(new GridLayout(2,4));
        for(int i=20;i<28;i++) subPanelFour.add(btnArray[i]);

        subPanelFive=new JPanel(new GridLayout(2,1));
        subPanelFive.add(btnArray[BTN_GROUP]);
        subPanelFive.add(btnArray[BTN_LOCK]);

        subPanelSix = new JPanel( new GridLayout(2,1));
        lblLoading = new JLabel("");
        lblLoading.setForeground(Color.blue);
        lblLoading.setVisible(false);
        progressBar = new JProgressBar(0,20);
        progressBar.setStringPainted(true);
        progressBar.setBorderPainted(true);
        progressBar.setVisible(false);
        subPanelSix.add(lblLoading);
        subPanelSix.add(progressBar);

        this.add(subPanelOne);
        this.add(subPanelTwo);
        this.add(subPanelThree);
        this.add(subPanelFour);
        this.add(subPanelFive);
        this.add(subPanelSix);

        for(int i=0;i<NUM_OF_BTNS;i++) btnArray[i].addActionListener(this);

        //initialization of selected btn;
        idOfDrawingBtn = BTN_ARROW;
        btnArray[idOfDrawingBtn].setSelected(true);

        isLocked = false;
    }

    public void setLoadingText(String str){
        //lblLoading.setVisible(true);
        //lblLoading.setText("");
        lblLoading.setText(str);
        lblLoading.paint(lblLoading.getGraphics());
    }

    public void setProgress(int val){
        this.progressBar.setValue( val );
        progressBar.paint(progressBar.getGraphics() );
        //{Rectangle barRectangle = null;
        //barRectangle = progressBar.getBounds();
        //barRectangle.x = 0;
        //barRectangle.y = 0;
        //progressBar.paintImmediately(barRectangle);}

        //{progressBar.invalidate();
        //progressBar.paintImmediately(progressBar.getBounds());
        //progressBar.validate();}
    }

    public int getIdOfDrawingBtn(){ return idOfDrawingBtn; }


    public void actionPerformed(ActionEvent evt){
        Object source = evt.getSource();

        if     (source == btnArray[BTN_ARROW] ) {
            unselectAllButtons();
            selectButton(BTN_ARROW);
            mainFrame.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        }
        else if(source==btnArray[BTN_ZOOM_WIN]) {
            unselectAllButtons();
            selectButton(BTN_ZOOM_WIN);
        }
        else if(source==btnArray[BTN_ZOOM_ALL]) {
            mainFrame.menuBar.alZoomAll.actionPerformed( evt );
            unselectAllButtons();
            btnArray[BTN_ZOOM_ALL].setSelected(false);
            btnArray[BTN_ARROW].setSelected(true);
            idOfDrawingBtn=BTN_ARROW;
        }
        else if(source==btnArray[BTN_ZOOM_PREV]) {
            mainFrame.menuBar.alZoomPrev.actionPerformed(evt);
            unselectAllButtons();
            btnArray[BTN_ZOOM_PREV].setSelected(false);
            btnArray[BTN_ARROW].setSelected(true);
            idOfDrawingBtn=BTN_ARROW;
        }


        else if(source == btnArray[BTN_SOURCE] ){ selectButton(BTN_SOURCE);}
        else if(source == btnArray[BTN_DG] )    { selectButton(BTN_DG);}
        else if(source == btnArray[BTN_BUS] )   { selectButton(BTN_BUS);}
        else if(source == btnArray[BTN_TXFM_3] )  { selectButton(BTN_TXFM_3);  }
        else if(source == btnArray[BTN_LINE] )  { selectButton(BTN_LINE);}
        else if(source == btnArray[BTN_CABLE] ) { selectButton(BTN_CABLE);}
        else if(source == btnArray[BTN_TXFM] )  { selectButton(BTN_TXFM);  }
        else if(source == btnArray[BTN_REG] )   { selectButton(BTN_REG);  }

        else if(source == btnArray[BTN_CKT_BK] ){ selectButton(BTN_CKT_BK);  }
        else if(source == btnArray[BTN_RECL] )  { selectButton(BTN_RECL);  }
        else if(source == btnArray[BTN_FUSE] )  { selectButton(BTN_FUSE);  }
        else if(source == btnArray[BTN_BK] )    { selectButton(BTN_BK);  }
        else if(source == btnArray[BTN_SECT] )  { selectButton(BTN_SECT);  }
        else if(source == btnArray[BTN_SWITCH] ){ selectButton(BTN_SWITCH);  }
        else if(source == btnArray[BTN_SWITCH_3] ){ selectButton(BTN_SWITCH_3);  }
        else if(source == btnArray[BTN_SWITCH_GND] ){ selectButton(BTN_SWITCH_GND);  }

        else if(source == btnArray[BTN_LOAD] )  { selectButton(BTN_LOAD);  }
        else if(source == btnArray[BTN_SERIES_CAP] )  { selectButton(BTN_SERIES_CAP);  }
        else if(source == btnArray[BTN_CAP] )  { selectButton(BTN_CAP);  }
        else if(source == btnArray[BTN_CT] )  { selectButton(BTN_CT);  }
        else if(source == btnArray[BTN_MOTOR] )  { selectButton(BTN_MOTOR);  }
        else if(source == btnArray[BTN_REACTOR] )  { selectButton(BTN_REACTOR); }
        else if(source == btnArray[BTN_ARRESTER] )  { selectButton(BTN_ARRESTER);  }
        else if(source == btnArray[BTN_PT] )  { selectButton(BTN_PT);  }

        else if(source == btnArray[BTN_GROUP] )  { selectButton(BTN_GROUP);  }
        else if(source == btnArray[BTN_LOCK] )  { toggleLock();}//selectButton(BTN_LOCK);  }
    }

    //only user click can change BTN_LOCK's status
    private void toggleLock(){
        // btnArray[BTN_LOCK].isSelected() can't be used to judge the button status
        // If doint so, this will always return true becoz this is called after
        // ActionPerformed() which sets the button status to be "selected".
        // So, we must need an attribute isSelected
        if( isLocked == true ){
            isLocked = false;
            btnArray[BTN_LOCK].setSelected(false);
            if( idOfDrawingBtn != BTN_ARROW){
                btnArray[idOfDrawingBtn].setSelected(false);
                btnArray[BTN_ARROW].setSelected(true);
                idOfDrawingBtn = BTN_ARROW;
            }
        }
        else{
            isLocked = true;
            btnArray[BTN_LOCK].setSelected(true);
        }
    }

    public boolean isLocked(){  return isLocked ; }

    public void selectButton( int btnId ){
        btnArray[idOfDrawingBtn].setSelected(false) ;
        btnArray[btnId].setSelected(true);
        idOfDrawingBtn=btnId;
    }

    public void unselectAllButtons(){
        for(int i=0;i<NUM_OF_BTNS;i++){
            btnArray[i].setSelected(false);
        }
        isLocked = false; //since unselected
    }

    public void enterBgImageMode( ){
        for(int i= BTN_SOURCE; i<=BTN_LOCK; i++ ){
            btnArray[i].setVisible( false );
        }
        unselectAllButtons();
        selectButton( BTN_ARROW);
    }

    public void enterBgLineMode( ){
        for(int i= BTN_SOURCE; i<=BTN_LOCK; i++ ){
            btnArray[i].setVisible( false );
        }
        unselectAllButtons();
        btnArray[BTN_LINE].setVisible(true);
        btnArray[BTN_LOCK].setVisible(true);
        selectButton( BTN_ARROW);
    }

    public void enterForeGroundMode(){
        for(int i= BTN_SOURCE; i<=BTN_LOCK; i++ ){
            btnArray[i].setVisible( true );
        }
        unselectAllButtons();
        selectButton( BTN_ARROW);
    }

}// end of ButtonPanel class